module rm {

	/**
	 * Create by richliu1023
	 * @date 2016-07-14
	 * @email richliu1023@gmail.com
	 * @github https://github.com/RichLiu1023
	 * @description a simple explanation
	 */
	export class UVSheetMethod {
		private _uvRectangle:egret3d.Rectangle = new egret3d.Rectangle();
		private _frameNum:number = 12;
		private _row:number = 4;
		private _column:number = 4;
		private _currentFrame:number = 0;
		private frameList:Array<egret3d.Rectangle> = [];
		private _material:egret3d.TextureMaterial;

		/**
		 * @language zh_CN
		 * 创建一个UV精灵动画的渲染方法对象
		 * @param frameNum  帧数量
		 * @param row 行数
		 * @param column 列数
		 * @param numTime 播放总时间
		 * @version Egret 3.0
		 * @platform Web,Native
		 */
		constructor( frameNum:number, row:number, column:number, material:egret3d.TextureMaterial ) {
			this._material = material;
			this.frameNum = frameNum;
			this.row = row;
			this.column = column;
			this.caculate();
			this.activeState();
		}

		public static create( frameNum:number,
							  row:number,
							  column:number,
							  material:egret3d.TextureMaterial ):UVSheetMethod {
			return new UVSheetMethod( frameNum, row, column, material );
		}

		private caculate() {
			this._uvRectangle.x = 0.0;
			this._uvRectangle.y = 0.0;
			this._uvRectangle.width = 1.0 / this._row;
			this._uvRectangle.height = 1.0 / this._column;
			this.frameList.length = this._frameNum;
			var rowIndex:number = 0;
			var columnIndex:number = 0;
			for ( var i:number = 0; i < this._frameNum; i++ ) {
				rowIndex = i % this._row;
				columnIndex = Math.floor( i / this._column );

				var rec:egret3d.Rectangle = new egret3d.Rectangle();
				rec.x = rowIndex * this._uvRectangle.width + this._uvRectangle.x;
				rec.y = columnIndex * this._uvRectangle.height + this._uvRectangle.y;
				rec.width = this._uvRectangle.width;
				rec.height = this._uvRectangle.height;
				this.frameList[ i ] = rec;
			}
		}

		/**
		 * @language zh_CN
		 * 设置帧数
		 * @param value 帧数
		 * @version Egret 3.0
		 * @platform Web,Native
		 */
		public set frameNum( value:number ) {
			if ( this._frameNum != value ) {
				this._frameNum = value;
			}
		}

		/**
		 * @language zh_CN
		 * 获取帧数
		 * @returns number  帧数
		 * @version Egret 3.0
		 * @platform Web,Native
		 */
		public get frameNum():number {
			return this._frameNum;
		}

		/**
		 * @language zh_CN
		 * 设置当前帧
		 * @param value 帧数
		 * @version Egret 3.0
		 * @platform Web,Native
		 */
		public set currentFrame( value:number ) {
			if ( this._currentFrame != value ) {
				if ( value > this._frameNum )return;
				this._currentFrame = value;
				this.activeState();
			}
		}

		/**
		 * 帧循环，如果在最后一帧调用，则返回第一帧。
		 * @returns {rm.UVSheetMethod}
		 */
		public nextFrame():rm.UVSheetMethod {
			let count = this.currentFrame + 1;
			if ( count >= this.frameNum ) {
				count = 0;
			}
			this.currentFrame = count;
			return this;
		}

		/**
		 * @language zh_CN
		 * 获取当前帧
		 * @returns number  帧数
		 * @version Egret 3.0
		 * @platform Web,Native
		 */
		public get currentFrame():number {
			return this._currentFrame;
		}

		/**
		 * @language zh_CN
		 * 设置行数
		 * @param value 行数
		 * @version Egret 3.0
		 * @platform Web,Native
		 */
		public set row( value:number ) {
			if ( this._row != value ) {
				this._row = value;
			}
		}

		/**
		 * @language zh_CN
		 * 获取行数
		 * @returns number  行数
		 * @version Egret 3.0
		 * @platform Web,Native
		 */
		public get row():number {
			return this._row;
		}

		/**
		 * @language zh_CN
		 * 设置列数
		 * @param value 动画列数
		 * @version Egret 3.0
		 * @platform Web,Native
		 */
		public set column( value:number ) {
			if ( this._column != value ) {
				this._column = value;
			}
		}

		/**
		 * @language zh_CN
		 * 获取列数
		 * @returns number  动画列数
		 * @version Egret 3.0
		 * @platform Web,Native
		 */
		public get column():number {
			return this._column;
		}

		/**
		 * @private
		 */
		public activeState() {
			var uvRectangle;
			if ( this.frameList.length == 0 ) {
				uvRectangle = this._uvRectangle;
			}
			else {
				uvRectangle = this.frameList[ this._currentFrame ];
			}
			this._material.uvRectangle = uvRectangle;
		}
	}
}